package com.hst.web.base.web;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.hst.core.dao.IORDao;
import com.hst.core.dao.ORQuery;
import com.hst.core.dao.ORQuery.Op;
import com.hst.core.json.JsonUtil;
import com.hst.core.meta.MetaRepository;
import com.hst.web.base.entity.TAccountIdentity;
import com.hst.web.base.entity.TAccountInfo;
import com.hst.web.base.ui.EmployeeIdentify;

@Controller
@RequestMapping("/identify")
public class IdentifyController {

	@Autowired
	IORDao dao;

	@Autowired
	MetaRepository mr;

	@SuppressWarnings("unchecked")
	@RequestMapping("/info")
	public void info(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
		Map<String, Object> params = JsonUtil.read(data, Map.class);
		String employeeCode = params.get("employeeCode").toString();
		ORQuery q = new ORQuery(Op.eq, "employeeCode", employeeCode);
		List<TAccountIdentity> list1 = dao.list(TAccountIdentity.class, q);
		EmployeeIdentify ei = null;
		if (list1.size() > 0) {
			ei = (EmployeeIdentify) mr.getMetaConvert("employee-identify").toMetaData(list1.get(0));
			List<TAccountInfo> list2 = dao.list(TAccountInfo.class, new ORQuery(Op.eq, "userName", ei.getUserName()));
			if (list2.size() > 0)
				ei.setPassword(list2.get(0).getPassword());
		}
		m.addAttribute("data", ei);
	}

}
